import {
  Format
} from '../../../utils/common'
import {
  Common
}
from "../../../utils/common"
import {
  OilModel
} from '../../../models/oil'
const oilmodel = new OilModel()
const app = getApp();
const format = new Format()
const common = new Common()
let last
Page({
  data: {
    TipTxtTwo: {
      tittle: '“北壳会员”未获取到您当前位置，这将导致您无法使用一键加油功能',
      leftBtn: '取消',
      rightBtn: '重新授权'
    }, // 位置授权拒绝后弹窗内容
    openTipPopTwoBtn: false, //是否显示位置授权拒绝后弹窗
    openTipPop: false, //提示弹窗
    TipTxt: '', //提示弹窗内容
    tips: '', //跳转判断
    station: '', // 油站信息
    goodsLists: '',
    selectOil: '', // 选中油枪号列表
    clickOilGunIndex: '-1', //选中油枪号样式
    clickOilNumIndex: '', // 选中油品样式
    inputDisabled: true, // 默认禁止输入金额
    btnDisabled: true, // 按钮
    payAmountInput: '', // 输入金额
    realAmount: '', // 金额分
    firstClick: true,
  },
  onLoad(options) {
    console.log('oilIndexonLoad' + JSON.stringify(options))
    this.getLocation()
  },
  onShow: function (options) {
    console.log('oilIndexonshow' + JSON.stringify(options))
    this.setData({
      firstClick: true,
    });
    if (!app.globalData.user || !app.globalData.user.cardAsn) {
      app.myLogin().then(r => {
        my.hideLoading();
        console.log(app.globalData.user)
      })
    }
    if (this.data.station && this.data.station.alipay_store_id) {
      this.getGoodsAll()
      this.setData({
        selectOil: '',
        clickOilGunIndex: '-1',
        clickOilNumIndex: '',
        inputDisabled: true,
        btnDisabled: true,
        payAmountInput: '',
        realAmount: '',
      });
    }
  },
  // 获取定位
  getLocation() {
    let vm = this
    app.getLocation().then(function(data) {
      // console.log(data)
      vm.setData({
        'station': data,
      })
      vm.getGoodsAll()
    },function(error) {
      if (error && (error.code || error.code == 0)) {
      console.log(error.code)
        if (error.code == 0) {
          vm.setData({
            openTipPopTwoBtn: true,
          });
        } else if (error.code == 1) {
          vm.setData({
            openTipPop: true,
            TipTxt: error.msg,
            tips: '',
          });
        } else if (error.code == 2) {
          vm.setData({
            openTipPop: true,
            TipTxt: error.msg,
            tips: '',
          });
        }
      }
    })
  },
  // 重新授权定位
  tipPopSureTwoBtn() {
    this.setData({
      openTipPopTwoBtn: false,
    });
    this.getLocation()
  },
  // 关闭双按钮弹窗
  tipPopCloseTwoBtn() {
    this.setData({
      openTipPopTwoBtn: false,
    });
  },
  // 点击顶部油站
  clickOil() {
    if (!this.data.station) {
      this.getLocation()
    }
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.getGoodsAll()
  },
  // 查询油枪
  getGoodsAll() {
    oilmodel.getGoodsAll(this.data.station.alipay_store_id).then(res => {
      // console.log(res)
      my.stopPullDownRefresh({
        complete(res) {
        }
      })
      if (res) {
        if (res.code == '00') {
          // res.goodsList[0].oilList[1] = '95#'
          this.setData({
            goodsLists: res.goodsList,
          });
        } else if (res.message) {
          this.setData({
            openTipPop: true,
            TipTxt: res.message,
            goodsLists: '',
            tips: 'close',
          });
        }
      } else {
        my.showToast({
          type: 'none',
          goodsLists: '',
          content: '查询油枪数量失败',
          duration: 1000,
        });
      }
    })
  },
  // 选择抢号
  tapOilGun(e) {
    const index = e.currentTarget.dataset.index
    const item = e.currentTarget.dataset.item
    if (!item.oilList) {
      my.showToast({
        type: 'none',
        content: '该油枪暂无交易或下拉刷新页面获取最新交易',
        duration: 2000,
      });
    } else {
      item.oilNum = item.oilList[0]
      this.setData({
          selectOil: item,
          clickOilGunIndex: index,
          inputDisabled: false,
      });
      console.log(this.data.selectOil)
    }
  },
  // 选择油号
  tapOilNum(e) {
    const index = e.currentTarget.dataset.index
    const item = e.currentTarget.dataset.item
    this.data.selectOil.oilNum = item
    this.setData({
        selectOil: this.data.selectOil,
        clickOilNumIndex: index,
    });
    // console.log(this.data.selectOil)
  },
  // 提示选择枪号弹窗
  tip() {
    if (this.data.inputDisabled) {
      my.showToast({
        type: 'none',
        content: '请选择枪号',
        duration: 1000,
      });
    }
  },
  // 输入金额
  onInput(e) {
    last = e.timeStamp
    var mesValue
    if (/^\d{1,5}(\.\d{0,2})?$/.test(e.detail.value)) {
      //满足条件
      mesValue = e.detail.value;
    } else {
      //不满足条件
      mesValue = e.detail.value.substring(0, e.detail.value.length - 1);
      my.showToast({
        type: 'none',
        content: '支付金额仅支持小数点前5位，小数点后2位',
        duration: 600,
      });
    }
    this.setData({
      payAmountInput: mesValue
    })
    setTimeout(() => {
      if (last - e.timeStamp == 0) {
        this.countAmount(mesValue)
      }
    }, 300);
  },
  onConfirm(e) {
    const value = this.data.payAmountInput
    this.countAmount(value)
  },
  // 金额
  countAmount(value) {
    this.setData({
      payAmountInput: value,
      realAmount: format.amount(value * 100), //显示的总金额
    })
    if (value) {
      this.setData({
        btnDisabled: false
      })
    } else {
      this.setData({
        btnDisabled: true
      })
    }
  },
  // 点击直接结算
  toPage(e) {
    let vm = this
    const page = e.currentTarget.dataset.url
    let selectOil = this.data.selectOil
    const amount = (this.data.payAmountInput * 100).toFixed(0)
    if (amount <= 0) {
      my.showToast({
        type: 'none',
        content: '加油金额必须大于0元',
        duration: 1000,
      });
      return
    }
    if (this.data.firstClick) {
      this.setData({
        firstClick: false,
      });
    } else {
      return
    }
    oilmodel.matchTradeByGoodsAndAmount(this.data.station.alipay_store_id,selectOil.oilGun,selectOil.oilNum,amount).then(res => {
      if (res) {
        if (res.code == '00') {
          res.tradeData.oilGun = selectOil.oilGun
          res.tradeData.oilNum = selectOil.oilNum
          // res.goodsList[0].oilList[1] = '95#'
          if (res.remake) {
            res.tradeData.remake = res.remake
          } else {
            res.tradeData.remake = ''
          }
          my.navigateTo({
            url: page + '?tradeData=' + encodeURIComponent(JSON.stringify(res.tradeData)),
            success: () => {
              vm.setData({
                selectOil: '',
                clickOilGunIndex: '-1',
                clickOilNumIndex: '',
                inputDisabled: true,
                btnDisabled: true,
                payAmountInput: '',
                realAmount: '',
              });
            }
          })
        } else if (res.message) {
          this.setData({
            openTipPop: true,
            TipTxt: res.message,
            tips: 'close',
          });
        }
      } else {
        vm.data.firstClick = true
        my.showToast({
          type: 'none',
          content: '匹配交易失败',
          duration: 1000,
        });
      }
    })
  },
  getUnpaidTrade() {
    let vm = this
    let selectOil = this.data.selectOil
    oilmodel.getUnpaidTrade().then(res => {
      if (res) {
        if (res.code == '00') {
          res.tradeData.oilGun = res.tradeData.oilGunNo
          // res.tradeData.oilNum = selectOil.oilNum
          // res.goodsList[0].oilList[1] = '95#'
          if (res.remake) {
            res.tradeData.remake = res.remake
          } else {
            res.tradeData.remake = ''
          }
          my.navigateTo({
            url: '/pages/oil/oilConfirm/oilConfirm?tradeData=' + encodeURIComponent(JSON.stringify(res.tradeData)) +  '&typeName=getUnpaidTrade',
            success: () => {
              vm.setData({
                selectOil: '',
                clickOilGunIndex: '-1',
                clickOilNumIndex: '',
                inputDisabled: true,
                btnDisabled: true,
                payAmountInput: '',
                realAmount: '',
              });
            }
          })
        } else if (res.message) {
          this.setData({
            openTipPop: true,
            TipTxt: res.message,
            tips: 'close',
          });
        }
      } else {
        vm.data.firstClick = true
        my.showToast({
          type: 'none',
          content: '获取未付款订单失败',
          duration: 1000,
        });
      }
    })
  },


  
  // 点击公共弹窗知道了
  tipPopSure(tips) {
    this.setData({
      firstClick: true,
      openTipPop: false
    });
    if (tips == 'href') {
      my.navigateBack('1')
    }
  },

});